import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

function pathResolve(dir: string) {
  return resolve(__dirname, ".", dir)
}

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd()) as ImportMetaEnv
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '/@': pathResolve('./src'),
      },
    },
    optimizeDeps: {
      include: [
        "@element-plus/icons",
        "dayjs"
      ]
    },
    server: {
      port: env.VITE_APP_PORT,
      proxy: {
        '/rest': {
          target: env.VITE_PROXY_REST_URL,  // 后台接口域名
          changeOrigin: true,  //是否跨域
          rewrite: (path) => path.replace(/^\/rest/, '/')
        },
        '/oauth': {
          target: env.VITE_PROXY_OAUTH_URL,  // 后台接口域名
          changeOrigin: true,  //是否跨域
          rewrite: (path) => path.replace(/^\/oauth/, '/oauth')
        },
        "/service-org": {
          target: env.VITE_PROXY_REST_URL,  // 后台接口域名
          changeOrigin: true,  //是否跨域
          rewrite: (path) => path.replace(/^\/service-org/, '/org')
        }
      }
    },
  }
})
